/******************************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals Dir, print*/
/*jslint plusplus: true, indent: 4*/

// This function is obsolete and kept for backward compatiblity only. Consider using Dir.entryList or Dir.entryListAdv function instead.
function getMaskFiles(aCurDir, aPrevDir, aFileMask, aDirMask, aRecursively, aDirsSort, aFilesSort, aPrintInfo) {
	"use strict";

	var i, j, d, prevDir, maskDirs, hasCurDir, curDirName, dList, newFiles,
		curDir = new Dir(aCurDir),
		foundFiles = [];

	print("The global function 'getMaskFiles' is obsolete and kept for backward compatiblity only. Consider using Dir.entryList or Dir.entryListAdv function instead.");

	if (aDirsSort === undefined) {
		aDirsSort = Dir.Name;
	}
	if (aFilesSort === undefined) {
		aFilesSort = Dir.Name;
	}

	if (aPrevDir.length !== 0) {
		prevDir = new Dir(aPrevDir);
		maskDirs = prevDir.entryList(aDirMask, Dir.Dirs, aDirsSort);
		hasCurDir = false;
		curDirName = curDir.name;
		for (i = 0; i < maskDirs.length; i++) {
			if (maskDirs[i] === curDirName) {
				hasCurDir = true;
				break;
			}
		}
		if (hasCurDir) {
			foundFiles = curDir.entryList(aFileMask, Dir.Files, aFilesSort);
			if (aPrintInfo) {
				print(foundFiles);
			}
			for (i = 0; i < foundFiles.length; i++) {
				foundFiles[i] = curDir.filePath(foundFiles[i]);
			}
			if (aPrintInfo) {
				print("In Dir" + " " + aCurDir + " Files Found " + foundFiles.length);
			}
		}
	}

    if (aRecursively) {
		dList = curDir.entryList("*", Dir.Dirs, aDirsSort);
		for (i = 0; i < dList.length; i++) {
			d = dList[i];
			/*jslint eqeq: true*/
			if (d != "." && d != "..") {
				newFiles = getMaskFiles(curDir.filePath(d), aCurDir, aFileMask, aDirMask, aRecursively, aDirsSort, aFilesSort);
				for (j = 0; j < newFiles.length; j++) {
					foundFiles.push(newFiles[j]);
				}
			}
			/*jslint eqeq: false*/
		}
	}
	return foundFiles;
}

// This function is obsolete and kept for backward compatiblity only. Consider using Dir.entryListAdv function instead.
/*jslint unparam: true*/
function getMaskFilesAdv(aDir, aPathMask, aDirsSort, aFilesSort, aPrintInfo) {
	"use strict";

	print("The global function 'getMaskFilesAdv' is obsolete and kept for backward compatiblity only. Consider using Dir.entryListAdv function instead.");

	return aDir.entryListAdv(aPathMask, aDirsSort, aFilesSort);
}
/*jslint unparam: false*/